package co.yixiang.yshop.module.product.controller.admin.shippingtemplates;

import co.yixiang.yshop.module.product.service.shippingtemplates.dto.ShippingTemplatesDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.product.controller.admin.shippingtemplates.vo.*;
import co.yixiang.yshop.module.product.dal.dataobject.shippingtemplates.ShippingTemplatesDO;
import co.yixiang.yshop.module.product.convert.shippingtemplates.ShippingTemplatesConvert;
import co.yixiang.yshop.module.product.service.shippingtemplates.ShippingTemplatesService;

@Tag(name = "管理后台 - 运费模板")
@RestController
@RequestMapping("/product/shipping-templates")
@Validated
public class ShippingTemplatesController {

    @Resource
    private ShippingTemplatesService shippingTemplatesService;

    @PostMapping("/create/{id}")
    @Operation(summary = "创建运费模板")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:create')")
    public CommonResult<Boolean> createShippingTemplates(@Validated @RequestBody ShippingTemplatesDTO shippingTemplatesDto, @PathVariable Integer id) {
        shippingTemplatesService.addAndUpdate(id, shippingTemplatesDto);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新运费模板")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:update')")
    public CommonResult<Boolean> updateShippingTemplates(@Valid @RequestBody ShippingTemplatesUpdateReqVO updateReqVO) {
        shippingTemplatesService.updateShippingTemplates(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运费模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:delete')")
    public CommonResult<Boolean> deleteShippingTemplates(@RequestParam("id") Integer id) {
        shippingTemplatesService.deleteShippingTemplates(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运费模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:query')")
    public CommonResult<ShippingTemplatesRespVO> getShippingTemplates(@RequestParam("id") Integer id) {
        ShippingTemplatesDO shippingTemplates = shippingTemplatesService.getShippingTemplates(id);
        return success(ShippingTemplatesConvert.INSTANCE.convert(shippingTemplates));
    }

    @GetMapping("/list")
    @Operation(summary = "获得运费模板列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:query')")
    public CommonResult<List<ShippingTemplatesRespVO>> getShippingTemplatesList(@RequestParam("ids") Collection<Integer> ids) {
        List<ShippingTemplatesDO> list = shippingTemplatesService.getShippingTemplatesList(ids);
        return success(ShippingTemplatesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运费模板分页")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:query')")
    public CommonResult<PageResult<ShippingTemplatesRespVO>> getShippingTemplatesPage(@Valid ShippingTemplatesPageReqVO pageVO) {
        PageResult<ShippingTemplatesDO> pageResult = shippingTemplatesService.getShippingTemplatesPage(pageVO);
        return success(ShippingTemplatesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运费模板 Excel")
    @PreAuthorize("@ss.hasPermission('product:shipping-templates:export')")
    @OperateLog(type = EXPORT)
    public void exportShippingTemplatesExcel(@Valid ShippingTemplatesExportReqVO exportReqVO,
                                             HttpServletResponse response) throws IOException {
        List<ShippingTemplatesDO> list = shippingTemplatesService.getShippingTemplatesList(exportReqVO);
        // 导出 Excel
        List<ShippingTemplatesExcelVO> datas = ShippingTemplatesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "运费模板.xls", "数据", ShippingTemplatesExcelVO.class, datas);
    }

}
